package com.pdwbdn.views;

import com.pdwbdn.db.AccountsDao;
import com.pdwbdn.db.LoginResult;
import com.pdwbdn.utils.DNFUtil;
import com.pdwbdn.utils.ExecUtil;
import org.apache.commons.configuration.PropertiesConfiguration;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame implements ActionListener {
    private final JTextField usernameField;
    private final JPasswordField passwordField;
    private long clickTimestamp;//记下点击时间防止双击

    public MainFrame(String title) {
        super(title);
        this.setSize(300, 200);
        this.setResizable(false);//不允许调整大小
        this.setLocationRelativeTo(null);//居中显示
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置默认关闭操作
        this.setLayout(new BorderLayout());

        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("注册|修改密码|忘记密码?");
        menu.setFont(MyFont.font14);
        menuBar.add(menu);
        JMenuItem regMenu = new JMenuItem("注册");
        regMenu.setFont(MyFont.font14);
        regMenu.setActionCommand("register");
        regMenu.addActionListener(this);
        menu.add(regMenu);
        JMenuItem modifyMenu = new JMenuItem("修改密码");
        modifyMenu.setFont(MyFont.font14);
        modifyMenu.setActionCommand("modify");
        modifyMenu.addActionListener(this);
        menu.add(modifyMenu);
        JMenuItem forgetMenu = new JMenuItem("忘记密码");
        forgetMenu.setFont(MyFont.font14);
        forgetMenu.setActionCommand("forget");
        forgetMenu.addActionListener(this);
        menu.add(forgetMenu);
        JMenuItem aboutMenu = new JMenuItem("关于");
        aboutMenu.setFont(MyFont.font14);
        aboutMenu.setActionCommand("about");
        aboutMenu.addActionListener(this);
        menu.add(aboutMenu);
        this.setJMenuBar(menuBar);

        String holderStr = "<html>&ensp;&ensp;&ensp;&ensp;</html>";
        this.getContentPane().add(new JLabel(holderStr), BorderLayout.NORTH);
        this.getContentPane().add(new JLabel(holderStr), BorderLayout.WEST);
        this.getContentPane().add(new JLabel(holderStr), BorderLayout.EAST);
        this.getContentPane().add(new JLabel(holderStr), BorderLayout.SOUTH);

        JPanel panel = new JPanel(new GridLayout(3, 1, 6, 6));
        Box usernameBox = Box.createHorizontalBox();
        JLabel usernameLabel = new JLabel("帐号：");
        usernameLabel.setFont(MyFont.font14);
        usernameField = new JTextField();
        usernameField.setFont(MyFont.font14);
        usernameBox.add(usernameLabel);
        usernameBox.add(usernameField);
        panel.add(usernameBox);

        Box passwordBox = Box.createHorizontalBox();
        JLabel passwordLabel = new JLabel("密码：");
        passwordLabel.setFont(MyFont.font14);
        passwordField = new JPasswordField();
        passwordField.setFont(MyFont.font14);
        passwordBox.add(passwordLabel);
        passwordBox.add(passwordField);
        panel.add(passwordBox);

        JButton button = new JButton("登录");
        button.setFont(MyFont.font14);
        button.setActionCommand("login");
        button.addActionListener(this);
        panel.add(button);

        this.getContentPane().add(panel, BorderLayout.CENTER);

        autoGetUsernamePassword();//初始化数据
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("register".equals(e.getActionCommand())) {
            RegisterDialog registerDialog = new RegisterDialog(this);
            registerDialog.setVisible(true);
        } else if ("modify".equals(e.getActionCommand())) {
            ModifyPwdDialog modifyPwdDialog = new ModifyPwdDialog(this);
            modifyPwdDialog.setVisible(true);
        } else if ("forget".equals(e.getActionCommand())) {
            ForgetPwdDialog forgetPwdDialog = new ForgetPwdDialog(this);
            forgetPwdDialog.setVisible(true);
        }  else if ("about".equals(e.getActionCommand())) {
            MsgDialog.getInstance().showMsg("<h1>dnf-launcher</h1><p>https://gitee.com/zz9532/dnf-launcher</p>");
        } else if ("login".equals(e.getActionCommand())) {
            try {
                long now = System.currentTimeMillis();
                if (Math.abs(now - clickTimestamp) < 3000) {
                    return;
                } else {
                    clickTimestamp = now;
                }
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                saveUsernamePassword(username, password);//记住用户名和密码
                LoginResult loginResult = AccountsDao.loginByPassword(username, password);
                if (loginResult.isSuccess()) {
                    String token = DNFUtil.createToken(loginResult.getUid());
                    String result = ExecUtil.exec("DNF.exe", token);
                    if ("ok".equals(result)) {
                        System.exit(0);
                    } else {
                        MsgDialog.getInstance().showMsg("启动失败");
                    }
                } else {
                    MsgDialog.getInstance().showMsg(loginResult.getMsg());
                }
            } catch (Throwable t) {
                MsgDialog.getInstance().showMsg(t.toString());
            }
        }
    }

    private void autoGetUsernamePassword() {
        try {
            PropertiesConfiguration config = new PropertiesConfiguration("dnf-launcher.properties");
            String username = config.getString("login.username");
            String password = config.getString("login.password");
            if (username != null) {
                usernameField.setText(username);
            }
            if (password != null) {
                passwordField.setText(password);
            }
        } catch (Throwable t) {
            MsgDialog.getInstance().showMsg(t.toString());
        }
    }

    private void saveUsernamePassword(String username, String password) {
        try {
            PropertiesConfiguration config = new PropertiesConfiguration("dnf-launcher.properties");
            config.setProperty("login.username", username);
            config.setProperty("login.password", password);
            config.save();
        } catch (Throwable t) {
            MsgDialog.getInstance().showMsg(t.toString());
        }
    }
}
